#!/usr/bin/env perl -w
# Time-stamp: <05 Oct 01 18:07:35 giron1> 

=head1 NAME

clearcase_uudecode.perl - uudecode and check files as compressed_file

=head1 SYNOPSIS

clearcase_uudecode.perl directory [0|1]

if the final flag is set to 0, it performs only a search on the
possible files, if set to 1, it executes the clearcase and uudecode
commands.

=head1 DESCRIPTION

This program parses the corresponding directory and subdirectories for
uuencoded files for files extensions defined in the file cvswrappers.

=head1 SEE ALSO


=head1 AUTHOR

 $Id: clearcase_uudecode.perl,v 1.1 2007/10/08 09:26:04 schweitz Exp $
 Franck Giron

=head1 LOG

 $Log: clearcase_uudecode.perl,v $
 Revision 1.1  2007/10/08 09:26:04  schweitz
 version 2.4, lib vergessen

 Revision /main/2  22-Jan-2003.10:03:51  giron1
 Changing header to /usr/bin/env perl.
 

=cut

use FileHandle;
use File::Basename;
#use File::stat;
use myFind;



=head1 FUNCTIONS

Here is a description of the corresponding functions

=head2 is_uuencoded($filename,\$mode)

Check if the file given by filename is uuencoded and returns the
corresponding mode defined in the header

=cut

sub is_uuencoded {
  my $filename = shift;
  my $mode_r = shift;
  
  my $res = 0;
  my $fp = new FileHandle "<$filename";
  if (not defined($fp)) {die "cannot open $filename";}

  my $basename = basename $filename;
  # Test the first bytes  uuencoded type, should be in the form
  # begin 740 $basename
  my $firstbytes = undef;
  # define a buffer length of at least the size of the basename
  # plus the size of 'begin 777 '
  my $length = length($basename) +  10;
  if (defined($fp->read($firstbytes,$length))) {
    if (defined($firstbytes)) {
      $res = ($firstbytes =~ m/^begin\s+(\d+)\s+$basename/);
      $$mode_r = $1;
    }
  }
  $fp->close;
  return $res
}

=head2 load_cvswrappers_entries()

Returns a list of files extensions defined in the file
'/cvs/speech/CVSROOT/cvswrappers'

=cut


sub load_cvswrappers_entries{

  my $filename = '/cvs/speech/CVSROOT/cvswrappers';

  my $fp = new FileHandle "<$filename";
  my @lines = <$fp>;
  $fp->close;

  my @entries = ();
  
  foreach (@lines) {
#    warn "$_";
    next if (m/^\s*\#/);
    # match the entries of the cvswrappers
    if (m/^\s*(\*\.\S+)\s+/) {push @entries,$1;}
  }
  
  return (@entries);
}

=head2 wanted($pat,$list)

function used by myFind to check for files verifying the pattern $pat

=cut

sub wanted {
    my ($pat,$list) = @_;
    my ($name);

    if (m/^(?:$pat)$/i) {
        $name = $myFind::name;
        push @{$list}, $name;
    }
}

# MAIN

my $rootfind = $ARGV[0];


# create a search pattern from the list
my @listfind = load_cvswrappers_entries();
foreach (@listfind) {
  s/\./\\\./;
  s/\*/.*/;
}
my $pattern = join('|',@listfind);

warn "$pattern\n";


# clearcase commands definition

# check out
my $co = "cleartool co -nc ";
# changing type to compressed_file
my $ct = "cleartool chtype -force compressed_file ";
#my $cmx = "cleartool protect -chmod 0777 ";
# changing file mode to 0... where ... is defined later
my $cm = "cleartool protect -chmod 0";
# uudecode command
my $uudecode = "/home/giron1/util/shtools/CVSBinDecode ";
# check in
my $ci = "cleartool ci -c 'uudecoded file' ";

$list = find(\&wanted,$pattern,$rootfind);
warn 'list=', join(' ',@$list), "\n";

my $do = $ARGV[1];  
my $mode = '';

foreach (@$list) {
  $mode = '';
  if (is_uuencoded($_,\$mode)) {
    $do and system($ct . $_);
    $do and system($co . $_);
#    use File::stat;
#    my $sb = stat($_);
#    printf "%s: uuencoded %04o\n",$_,$sb->mode & 07777;
    printf "%s: uuencoded %s\n",$_,$mode;
    $do and system($uudecode . $_);
#    chmod $sb->mode & 07777, $_;
    $do and system($cm . $mode . ' ' . $_);
    $do and system($ci . $_);
  }
  else {print "$_\n";}
}
